package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.GongShiDaoImpl;
import com.ysd.entity.GongShi;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminGongSiServlet extends BaseServlet {
	/**
	 * 公司信息
	 * localhost:8080/shangcheng/adminGongSi?reqName=getGongSi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getGongSi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GongShiDaoImpl sDao=new GongShiDaoImpl();
		List<GongShi> gsList=sDao.getGongSi();
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out=response.getWriter();//out对象
		
		Gson gson=new Gson();
		out.print(gson.toJson(gsList));
		
		out.close();
		out.flush();
	}
	/**
	 * 公司信息
	 * localhost:8080/shangcheng/adminGongSi?reqName=getGS
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getGS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GongShiDaoImpl sDao=new GongShiDaoImpl();
		GongShi gs=sDao.getGongShi();
		
		response.setCharacterEncoding("UTF-8");//设置响应编码格式
		PrintWriter out=response.getWriter();//out对象
		
		Gson gson=new Gson();
		out.print(gson.toJson(gs));
		
		out.close();
		out.flush();
	}

	
	/**
	 * 修改公司信息
	 * localhost:8080/shangcheng/adminGongSi?reqName=upDateGS
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upDateGS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		PrintWriter out= response.getWriter();
		
		GongShiDaoImpl gsDao=new GongShiDaoImpl();
		String logo=request.getParameter("logo");
		String name=request.getParameter("gsName");
		String diZhi=request.getParameter("wangye");
		String gSjieShao=request.getParameter("jieshao");
		String dianHua=request.getParameter("dianhua");
		String email=request.getParameter("youxiang");
		String gSDiZhi=request.getParameter("dizhi");
		String qiYeWenHua=request.getParameter("wenhua");
		GongShi gs=new GongShi(logo, name, diZhi, gSjieShao, dianHua, email, gSDiZhi, qiYeWenHua);
		int n=gsDao.updateGongSi(gs);
		if(n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		}else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		out.flush();
		out.close();
	}
	
	/**
	 * 添加图片
	 * localhost:8080/shangcheng/adminGongSi?reqName=upload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out= response.getWriter();//out对象
		
		//创建smartUpload对象
		SmartUpload smart=new SmartUpload();
		//设置编码格式
		smart.setCharset("UTF-8");
		//初始化
		smart.initialize(getServletConfig(),request,response);
		
		try {
			//上传
			smart.upload();
			//设置上传文件名
			String fileName=System.currentTimeMillis()+".jpg";
			//将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("images/"+fileName);
			//响应数据
			out.print(Result.toClient(true, "上传成功",fileName));
		}catch(SmartUploadException e){
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
	}
}
